const Router = require('koa-router');
const router = new Router();
const tools = require("../../tools/tool");
const {db, QueryTypes} = require("../../models/db");
const tool = require("../../tools/tool");

// 其他应收款列表
router.post('/list/qtzc', async ctx => {
    try {
        let tenantId = ctx.user.tenantId
        let body = ctx.request.body
        let formData = body.formData
        let excel = body.excel
        let beginDate = formData.beginDate
        let endDate = formData.endDate
        let billNo = formData.billNo
        let buName = formData.buName
        let billState = formData.billState
        let sql = `select * from v_main where tenantId=${tenantId} and transType=153402 `
        if(beginDate) {
            sql += ` and billDate >='${beginDate}' `
        }
        if(endDate) {
            sql += ` and billDate<='${endDate}' `
        }
        if(billNo) {
            sql += ` and billNo like '${billNo}' `
        }
        if(buName) {
            sql += ` and buName like '${buName}'  `
        }
        if(billState === 'all') {
            sql += ` and billState in('draft','accounted') `
        }
        if(billState === 'draft') {
            sql += ` and billState='draft' `
        }
        if(billState === 'accounted') {
            sql += ` and billState='accounted' `
        }
        sql += ' order by billStateSort,billDate '
        let list = await db.query(sql, {type: QueryTypes.SELECT})
        if(excel) {
            return tool.doExcelOut(ctx,list,)
        }

        return ctx.suc('', {
            list
        })
    } catch (e) {
        return ctx.err(e);
    }
})


module.exports = router;